package com.qsboss.demo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qsboss.demo.domain.Result;
import com.qsboss.demo.mapper.TbUserMapper;
import com.qsboss.demo.pojo.TbUser;
import com.qsboss.demo.service.TbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: 用户信息服务实现类
 * @date 2023/10/22 16:29
 */

@Service
public class TbUserServiceImpl implements TbUserService {

    @Autowired
    TbUserMapper tbUserMapper;

    @Override
    public Result login(String userName, String userPass) {

        // 获取用户信息
        TbUser tbUser = tbUserMapper.getUserByName(userName);
        if(tbUser == null) {
            return Result.FAILED(404, "用户不存在");
        }

        // 验证密码
        if(!tbUser.getUserPass().equals(userPass)) {
            return Result.FAILED(403, "用户密码不正确");
        }

        // 返回用户信息
        tbUser.setUserPass(null);

        // 设置用户角色名称
        tbUser.setRoleName(getRoleName(tbUser.getUserRole()));

        return Result.SUCCESS(tbUser);
    }

    @Override
    public List<TbUser> userList() {

        List<TbUser> userList = tbUserMapper.getUserList();

        return userList;
    }

    @Override
    public String getRoleName(String role) {
        if(role.equals("admin")) {
            return "管理员";
        }
        else if(role.equals("employee")) {
            return "财务人员";
        }
        else if(role.equals("manager")) {
            return "财务经理";
        }
        else if(role.equals("customer")) {
            return "客户";
        }

        return "未知";
    }

    @Override
    public PageInfo getUserByPage(int curPage, int pageSize) {

        PageHelper.startPage(curPage, pageSize);
        Page<TbUser> userByPage = tbUserMapper.getUserByPage();

        // 填充用户角色
        userByPage.getResult().forEach(item -> {
            item.setRoleName(getRoleName(item.getUserRole()));
        });

        return new PageInfo(userByPage);
    }

    @Override
    public PageInfo getCustomerByPage(int curPage, int pageSize) {
        PageHelper.startPage(curPage, pageSize);
        Page<TbUser> userByPage = tbUserMapper.getCustomerByPage();

        // 填充用户角色
        userByPage.getResult().forEach(item -> {
            item.setRoleName(getRoleName(item.getUserRole()));
        });

        return new PageInfo(userByPage);
    }

    @Override
    public TbUser getUserByMail(String userMail) {

        return tbUserMapper.getUserByMail(userMail);
    }

    @Override
    public TbUser getUserById(Integer customerId) {
        return tbUserMapper.getUserById(customerId);
    }
}
